package trss.model.torrent;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;


import trss.bencode.BDictionary;
import trss.bencode.BElement;
import trss.bencode.BList;
import trss.bencode.Util;

@Entity
@Table(name = "metafile_info")
public class Info {
    private String name = "";
    private Long length = null;
    private String md5summ = null;
    private boolean multifile = false;
    private List<File> files = new ArrayList<File>();
    private Long id;
    
    public Info() {}

    /**
     * @param dict
     *            the BDictionary
     */
    public Info(BDictionary dict) {
        name = Util.getString(dict.get("name"));
        length = Util.getLong(dict.get("length"));
        md5summ = Util.getString(dict.get("md5summ"));
        if (length == null) { // multifile
            multifile = true;
            BList files = (BList) dict.get("files");
            for (BElement bElement : files) {
                this.files.add(new File((BDictionary) bElement));
            }
        }

    }

    /**
     * @return the name
     */
    @Column
    //@SearchableProperty
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the length
     */
    @Column
    public Long getLength() {
        return length;
    }

    /**
     * @param length
     *            the length to set
     */
    public void setLength(Long length) {
        this.length = length;
    }

    /**
     * @return the md5summ
     */
    @Column
//    @SearchableProperty
    public String getMd5summ() {
        return md5summ;
    }

    /**
     * @param md5summ
     *            the md5summ to set
     */
    public void setMd5summ(String md5summ) {
        this.md5summ = md5summ;
    }

    /**
     * @return the multifile
     */
    @Transient
    public boolean isMultifile() {
        return multifile;
    }

    /**
     * @param multifile
     *            the multifile to set
     */
    public void setMultifile(boolean multifile) {
        this.multifile = multifile;
    }

    /**
     * @return the files
     */
    @OneToMany
    public List<File> getFiles() {
        return files;
    }

    /**
     * @param files
     *            the files to set
     */
    public void setFiles(List<File> files) {
        this.files = files;
    }

    /**
     * @return the id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
//    @SearchableId
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public String toString() {
        return "Info [name=" + name + ", length=" + length + ", md5summ="
                + md5summ + ", multifile=" + multifile + ", files=" + files
                + "]";
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((files == null) ? 0 : files.hashCode());
        result = prime * result + ((length == null) ? 0 : length.hashCode());
        result = prime * result + ((md5summ == null) ? 0 : md5summ.hashCode());
        result = prime * result + (multifile ? 1231 : 1237);
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Info other = (Info) obj;
        if (files == null) {
            if (other.files != null) {
                return false;
            }
        } else if (!files.equals(other.files)) {
            return false;
        }
        if (length == null) {
            if (other.length != null) {
                return false;
            }
        } else if (!length.equals(other.length)) {
            return false;
        }
        if (md5summ == null) {
            if (other.md5summ != null) {
                return false;
            }
        } else if (!md5summ.equals(other.md5summ)) {
            return false;
        }
        if (multifile != other.multifile) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }

}
